package com.ruoyi.web.controller.wx_warehouseman;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.vqms.domain.VQMSQueue;
import com.ruoyi.vqms.domain.VQMSQueueArea;
import com.ruoyi.vqms.service.IVQMSLedService;
import com.ruoyi.vqms.service.IVQMSQueueAreaService;
import com.ruoyi.vqms.service.IVQMSQueueReservationService;
import com.ruoyi.vqms.service.IVQMSQueueService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 仓管员排队信息
 */
@RestController
@RequestMapping("/wx/warehouseman/queue")
public class warehouseQueueController extends BaseController {

    @Autowired
    private IVQMSQueueService queueService;

    @Autowired
    private IVQMSQueueAreaService areaService;

    @Autowired
    private IVQMSQueueReservationService queueReservationService;

    @Autowired
    IVQMSLedService ledService;
    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询排队列表
     */
    @RequestMapping("/list")
    public TableDataInfo list(VQMSQueue vqmsQueue) {

        try {
            if (CollectionUtils.isEmpty(vqmsQueue.getLocationList())){
                return getDataTable(Collections.emptyList());
            }
            startPage();
            List<VQMSQueue> queueList = queueService.warehouseSelectList(vqmsQueue,vqmsQueue.getLocationList());
            return getDataTable(queueList);
        } catch (Exception e) {
//            log.error("查询排队任务异常", e);
            TableDataInfo rspData = new TableDataInfo();
            rspData.setCode(HttpStatus.ERROR);
            rspData.setMsg("未分配库位");
            return rspData;
        }

//        try {
//            startPage();
//            //仓管员查询当天排队信息
//            List<VQMSQueue> list=queueService.warehouseSelectList(vqmsQueue);
//            List<VQMSQueue> queueList = new LinkedList<>();
//            if (vqmsQueue.getLocationList().size() > 0){        //判断仓管员是否绑定有库位
//                for (int i = 0;i<vqmsQueue.getLocationList().size();i++){
//                    for (VQMSQueue vqmsQueue1 : list){
//                        if (Objects.equals(vqmsQueue1.getStorehouseLocationName(), vqmsQueue.getLocationList().get(i))){        //符合仓管员绑定库位（getStorehouseLocationName库位名称）
//                            queueList.add(vqmsQueue1);
//                        }
//                    }
//                }
//            }
//            if (!queueList.isEmpty()){  //存在符合条件的排队信息
//                // 根据排队序号从小到大排序
//                queueList.sort(Comparator.comparingInt(VQMSQueue::getQueueIndex));
//            }
//            return getDataTable(queueList);
//        } catch (Exception e){
//            TableDataInfo rspData = new TableDataInfo();
//            rspData.setCode(HttpStatus.ERROR);
//            rspData.setMsg("未分配库位");
//            return rspData;
//        }
    }

    /**
     * 获取排队队列列表
     * @param vqmsQueueArea
     * @return
     */
    @RequestMapping("/getList")
    public AjaxResult getAreaList(VQMSQueueArea vqmsQueueArea){
        vqmsQueueArea.setStatus(1); //默认正常
        return AjaxResult.success(areaService.selectList(vqmsQueueArea));
    }

    /**
     * 装卸货中的完成装卸货
     */
    @PostMapping(value = "/next")
    public AjaxResult next(VQMSQueue vqmsQueue)
    {

        SysUser sysUser = sysUserService.selectUserById(SecurityUtils.getUserId());
        vqmsQueue.setUpdateBy(sysUser.getNickName());
        queueService.next(vqmsQueue);

        //刷新LED大屏
        try {
            ledService.flushLesScreenData(vqmsQueue.getAreaId());
        } catch (Exception e) {
            logger.error("排队下一个刷新LED大屏失败");
            e.printStackTrace();
        }

        return AjaxResult.success();
    }

    /**
     * 排队中的完成装卸货
     */
    @PostMapping(value = "/queueNext")
    public AjaxResult queueNext(VQMSQueue vqmsQueue)
    {

        SysUser sysUser = sysUserService.selectUserById(SecurityUtils.getUserId());
        vqmsQueue.setUpdateBy(sysUser.getNickName());
        queueService.queueNext(vqmsQueue);

        //刷新LED大屏
        try {
            ledService.flushLesScreenData(vqmsQueue.getAreaId());
        } catch (Exception e) {
            logger.error("排队下一个刷新LED大屏失败");
            e.printStackTrace();
        }

        return AjaxResult.success();
    }

    /**
     * 排队更换队列
     */
    @PostMapping(value = "/change")
    public AjaxResult change(VQMSQueue vqmsQueue)
    {
        SysUser sysUser = sysUserService.selectUserById(SecurityUtils.getUserId());
        vqmsQueue.setUpdateBy(sysUser.getNickName());
        queueService.change(vqmsQueue);

        //刷新LED大屏
        try {
            ledService.flushLesScreenData(vqmsQueue.getAreaId());
        } catch (Exception e) {
            logger.error("排队更换队列刷新LED大屏失败");
            e.printStackTrace();
        }

        return AjaxResult.success();
    }

    /**
     * 删除排队
     */
    @GetMapping(value = "/delete/{ids}")
    public AjaxResult delete(@PathVariable Long[] ids) {
        return AjaxResult.success(queueReservationService.delete(ids));
    }

    /**
     * 排队跳过（当前排队与下一条对调）
     */
    @PostMapping(value = "/skip")
    public AjaxResult skip(VQMSQueue vqmsQueue)
    {
        SysUser sysUser = sysUserService.selectUserById(SecurityUtils.getUserId());
        vqmsQueue.setUpdateBy(sysUser.getNickName());
        queueService.skip(vqmsQueue);

        //刷新LED大屏
        try {
            ledService.flushLesScreenData(vqmsQueue.getAreaId());
        } catch (Exception e) {
            logger.error("排队置顶刷新LED大屏失败");
            e.printStackTrace();
        }

        return AjaxResult.success();
    }

    /**
     * 排队置顶
     */
    @PostMapping(value = "/topping")
    public AjaxResult topping(VQMSQueue vqmsQueue)
    {
        SysUser sysUser = sysUserService.selectUserById(SecurityUtils.getUserId());
        vqmsQueue.setUpdateBy(sysUser.getNickName());
        try {
            queueService.warehouseTopping(vqmsQueue);
        } catch (Exception e) {
           return AjaxResult.error("置顶-当天排队信息异常");
        }

        //刷新LED大屏
        try {
            ledService.flushLesScreenData(vqmsQueue.getAreaId());
        } catch (Exception e) {
            logger.error("排队置顶刷新LED大屏失败");
            e.printStackTrace();
        }

        return AjaxResult.success();
    }

}
